package com.wisdudu.blank.support.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;

import com.wisdudu.blank.R;
import com.wisdudu.blank.app.App;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by qyf on 2016/3/12.
 * 枚举单例改写 使用方法：ToastUtil.INSTANCE.show("显示内容")
 */
public enum ToastUtil {
    INSTANCE;
    private Toast toast;
//    private TextView mTv;

    @SuppressLint("ShowToast")
    public void init(Context context) {
        toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);

//        View v = toast.getView();
//        mTv = (TextView) v.findViewById(android.R.id.message);
    }

    private void show(CharSequence content, int duration) {
        if (content.length() != 0) {
            AndroidSchedulers.mainThread().createWorker().schedule(() -> {
                toast.setText(content);
                toast.setDuration(duration);
                toast.show();
            });
        }
    }

    private void show(CharSequence content) {
        show(content, Toast.LENGTH_SHORT);
    }

    public void toast(String text) {
        show(text);
    }

    public void toastLong(String text) {
        show(text, Toast.LENGTH_LONG);
    }


    /**
     * 接口请求失败，网络连接中断，提示请检查网络连接
     * 非网络连接中断问题，提示服务器无法正常连接，此种情况需要重视解决
     * 用于onError中判断
     */
    public void netError() {
        if (!NetUtil.INSTANCE.isConnected()) {
            show(App.getContext().getResources().getString(R.string.net_error));
        } else {
            show(App.getContext().getResources().getString(R.string.server_cannt_link));
        }
    }


}
